package com.example.weather

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.weather.weather.Forecast
import com.example.weather.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {
    val baseURL="http://t.weather.itboy.net/api/weather/city/"
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val cityCode = intent.getStringExtra("city_code")
        val queue = Volley.newRequestQueue(this)
        val stringRequest = StringRequest(baseURL+cityCode,{
            val gson = Gson()
            val WeatherType= object : TypeToken<Weather>(){}.type
            val weather:Weather = gson.fromJson(it,WeatherType)

            val textView_city = findViewById<android.widget.TextView>(R.id.textView)
            textView_city.text = weather.cityInfo.city
            val province=findViewById<android.widget.TextView>(R.id.textView_province)
            province.text = weather.cityInfo.parent
            val temperature=findViewById<android.widget.TextView>(R.id.textView_wendu)
            temperature.text = weather.data.wendu+"°C"
            val humidity=findViewById<android.widget.TextView>(R.id.textView_shidu)
            humidity.text = weather.data.shidu

            val firstDay: Forecast = weather.data.forecast.first()
            when(firstDay.type){
                "晴"->findViewById<android.widget.ImageView>(R.id.imageView).setImageResource(R.drawable.sun)
                "多云"->findViewById<android.widget.ImageView>(R.id.imageView).setImageResource(R.drawable.cloud)
                "小雨"->findViewById<android.widget.ImageView>(R.id.imageView).setImageResource(R.drawable.rain)
                "阴"->findViewById<android.widget.ImageView>(R.id.imageView).setImageResource(R.drawable.mcloud)
                "雪"->findViewById<android.widget.ImageView>(R.id.imageView).setImageResource(R.drawable.snow)
                else->findViewById<android.widget.ImageView>(R.id.imageView).setImageResource(R.drawable.thunder)
            }
            val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1,weather.data.forecast)
            val listView = findViewById<android.widget.ListView>(R.id.listView)
            listView.adapter = adapter


            Log.d("MainActivity2", weather.cityInfo.city)
            Log.d("MainActivity2", "${weather.cityInfo.parent}")
        },{
            Log.d("MainActivity2", "Error: $it")
        })
        queue.add(stringRequest)
    }
}